package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author jiangjie
 * @version 1.0
 * @date 2023/10/12 14:06
 */
@Component
public class MybatisPlusConfig implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {

        try {
            this.setFieldValByName("createBy", SecurityUtils.getUserId().toString(),metaObject);
            this.setFieldValByName("updateBy", SecurityUtils.getUserId().toString(),metaObject);
        } catch (Exception e) {
            this.setFieldValByName("createBy", "匿名",metaObject);
            this.setFieldValByName("updateBy", "匿名",metaObject);
        }
        this.setFieldValByName("createTime", new Date(),metaObject);
        this.setFieldValByName("updateTime", new Date(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {

        try {
            this.setFieldValByName("updateBy", SecurityUtils.getUserId(),metaObject);
        } catch (Exception e) {
            this.setFieldValByName("updateBy", "匿名",metaObject);
        }
        this.setFieldValByName("updateTime", new Date(),metaObject);
    }
}
